package utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.peng.auctionsystem.R;

import base.BaseApplication;


/**
 * Created by Mr.Q on 2018/3/10.
 * 描述
 */

public class ToastUtil {


    private static Toast toast;
    private static Toast toast2;

    private static Toast initToast(CharSequence message, int duration) {
        if (toast == null) {
            toast = Toast.makeText(BaseApplication.getAppContext(), message, duration);
        } else {
            toast.setText(message);
            toast.setDuration(duration);
        }
        return toast;
    }

    /**
     * 短时间显示Toast
     *
     * @param message
     */
    public static void showShort(CharSequence message) {
        initToast(message, Toast.LENGTH_SHORT).show();
    }


    /**
     * 短时间显示Toast
     *
     * @param strResId
     */
    public static void showShort(int strResId) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_SHORT).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param message
     */
    public static void showLong(CharSequence message) {
        initToast(message, Toast.LENGTH_LONG).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param strResId
     */
    public static void showLong(int strResId) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_LONG).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param message
     * @param duration
     */
    public static void show(CharSequence message, int duration) {
        initToast(message, duration).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param strResId
     * @param duration
     */
    public static void show(Context context, int strResId, int duration) {
        initToast(context.getResources().getText(strResId), duration).show();
    }

    /**
     * 显示有image的toast
     *
     * @param tvStr
     * @param imageResource
     * @return
     */
    public static Toast showToastWithImg(final String tvStr, final int imageResource) {
        if (toast2 == null) {
            toast2 = new Toast(BaseApplication.getAppContext());
        }
        Context context = BaseApplication.getAppContext();
        LinearLayout root = new LinearLayout(context);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setBackgroundResource(R.drawable.tv_rounded_corners);
        root.setAlpha(0.7f);
        root.setGravity(Gravity.CENTER);
        int screenWidth = ScreenUtil.getScreenWidth(context);
        int screenHeight = ScreenUtil.getScreenHeight(context);


        ImageView iv = new ImageView(context);
        int ivPadding = (int) (52.0f / 1275 * screenHeight);
        iv.setPadding(0, ivPadding, 0, ivPadding);
        root.addView(iv);

        TextView tv = new TextView(context);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        tv.setTextColor(0xffffffff);

        LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvParams.gravity = Gravity.CENTER;
        if (imageResource > 0) {
            int width = (int) (266.0f / 720 * screenWidth);
            root.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
            root.setMinimumWidth(width);
            iv.setVisibility(View.VISIBLE);
            iv.setImageResource(imageResource);

            int bottom = (int) (36.0f / 1275 * screenHeight);
            tvParams.setMargins(0, 0, 0, bottom);

        } else {
            int tvPadding = (int) (30.0f / 1275 * screenHeight);
            tvParams.setMargins(0, tvPadding, 0, tvPadding);
            iv.setVisibility(View.GONE);

        }
        int padding = (int) (25.0f / 720 * screenWidth);
        root.setPadding(padding, 0, padding, 0);

        root.addView(tv, tvParams);
        tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);

        toast2.setView(root);
        toast2.setGravity(Gravity.CENTER, 0, 0);
        toast2.setDuration(Toast.LENGTH_LONG);
        toast2.show();
        return toast2;

    }

    /**
     * 显示有image的toast
     *
     * @param tvStr
     * @param imageResource
     * @return
     */
    public static Toast showToastWithImgShort(final String tvStr, final int imageResource) {
        if (toast2 == null) {
            toast2 = new Toast(BaseApplication.getAppContext());
        }
        Context context = BaseApplication.getAppContext();
        LinearLayout root = new LinearLayout(context);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setBackgroundResource(R.drawable.tv_rounded_corners);
        root.setAlpha(0.7f);
        root.setGravity(Gravity.CENTER);
        int screenWidth = ScreenUtil.getScreenWidth(context);
        int screenHeight = ScreenUtil.getScreenHeight(context);


        ImageView iv = new ImageView(context);
        int ivPadding = (int) (52.0f / 1275 * screenHeight);
        iv.setPadding(0, ivPadding, 0, ivPadding);
        root.addView(iv);

        TextView tv = new TextView(context);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        tv.setTextColor(0xffffffff);

        LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvParams.gravity = Gravity.CENTER;
        if (imageResource > 0) {
            int width = (int) (266.0f / 720 * screenWidth);
            root.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
            root.setMinimumWidth(width);
            iv.setVisibility(View.VISIBLE);
            iv.setImageResource(imageResource);

            int bottom = (int) (36.0f / 1275 * screenHeight);
            tvParams.setMargins(0, 0, 0, bottom);

        } else {
            int tvPadding = (int) (30.0f / 1275 * screenHeight);
            tvParams.setMargins(0, tvPadding, 0, tvPadding);
            iv.setVisibility(View.GONE);

        }
        int padding = (int) (25.0f / 720 * screenWidth);
        root.setPadding(padding, 0, padding, 0);

        root.addView(tv, tvParams);
        tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);

        toast2.setView(root);
        toast2.setGravity(Gravity.CENTER, 0, 0);
        toast2.setDuration(Toast.LENGTH_SHORT);
        toast2.show();
        return toast2;

    }
}

